package com.qianlou.springai01chat.controller;


import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.ai.openai.api.OpenAiImageApi;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ImageController {

    @Resource
    private OpenAiImageModel openAiImageModel;


    @RequestMapping("/ai/image")
    public String getImage(@RequestParam("msg") String msg) {
        OpenAiImageOptions options = OpenAiImageOptions.builder()
                .withModel(OpenAiImageApi.ImageModel.DALL_E_3.getValue())
                .withQuality("hd")
                .withN(1)
                .withHeight(1024)
                .withWidth(1024)
                .build();
        ImageResponse imageResponse = openAiImageModel.call(new ImagePrompt(msg, options));
        String url = imageResponse.getResult().getOutput().getUrl();
        url = "<img src=\"" + url + "\" />";
        return url;
    }
}
